package com.starbaba.stat.view;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.base.common.util.ObjectUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.stat.model.RegionVO;
import com.starbaba.stat.model.StatModel;
import com.starbaba.stat.service.StatService;
import com.starbaba.stat.utils.RegionInfoHandler;

@SuppressWarnings("rawtypes")
public class StatAction extends BaseAction implements ModelDriven {
    /**
	 * 
	 */
    private static final long serialVersionUID = -1629639856220872834L;

    private StatModel model;

    private String fileName;

    private StatService server;

    private InputStream input;

    public String getFileName() {
        return fileName;
    }

    public String showPage() {
        paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(),
                paramsMap);
        String searchType = (String) paramsMap.get("searchType");
        String interface_id = (String) paramsMap.get("interface_id");
        model.setSearchType(searchType);
        model.setInterface_id(interface_id);
        model.setProvinceList(RegionInfoHandler.getInstance().getPrivonce(getService()));
        model.setCityList(new ArrayList<RegionVO>());
        return "success";
    }

    public String exportcode() throws IOException {

        paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(),
                paramsMap);
        // 操作类别：0为全部订单，1未完成订单，2为成功订单，3为失败订单
        // model.setType(null);
        //
        // String tmp = (String) paramsMap.get("channel");
        // if (null != tmp && tmp.length() > 0) {
        // channel = new String(tmp.getBytes("iso-8859-1"), "UTF-8");
        // model.setChannel(channel);
        // }
        // String value = service.exportCVS(channel);
        //
        // input = new ByteArrayInputStream(value.getBytes("GBK"));
        // fileName = channel + System.currentTimeMillis() + ".csv";

        String interface_id = (String) paramsMap.get("interface");
        paramsMap.put("interface_id", interface_id);
        paramsMap.remove("interface");
        paramsMap.remove("test");
        Map<String, Map<String, Object>> dataMap = new HashMap<String, Map<String, Object>>();
        dataMap.put("databean", paramsMap);
        String request = ObjectUtils.toJsonString(dataMap);
        try {
            String exportResult = server.getExportString(request, interface_id);
            input = new ByteArrayInputStream(exportResult.getBytes("GBK"));
            fileName = "Prd_detail_" + System.currentTimeMillis() + ".csv";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";

    }

    public String exportPage() throws IOException {
    	
    	paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(),paramsMap);
    	String interface_id = (String) paramsMap.get("interface");

    	paramsMap.remove("interface");
    	paramsMap.put("interface_id", interface_id);
    	
		Map<String, Map<String, Object>> dataMap = new HashMap<String, Map<String, Object>>();
		dataMap.put("databean", paramsMap);
		String request = ObjectUtils.toJsonString(dataMap);
		try {
			String exportResult = server.exportString(request, interface_id);
			input = new ByteArrayInputStream(exportResult.getBytes("GBK"));
			fileName = "Detail_" + System.currentTimeMillis() + ".csv";
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "success";
    }
    
    public InputStream getInputStream() {
        return input;
    }

    @Autowired
    @Qualifier("statServiceImpl")
    public void setServer(StatService server) {
        this.server = server;
    }

    @Override
    public Object getModel() {
        if (model == null) {
            model = new StatModel();
        }
        return model;
    }

}
